
#include <RTL.h>
#include <stdio.h>

#include "stm32f10x.h"

#include "Chip_Pin.h"
#include "uart_drv.h"
#include "MyLib.h"

#define __MAIN_C__
#include "main.h"

OS_TID t1;
OS_TID t2;
OS_TID t3;
OS_TID t4;

void OSTimeDly(u32 c)
{
	os_dly_wait(c);
}


__task void phaseA (void)  
{
	int i = 0;
	for (;;) {
	    printf("task A %d\r\n",i++);
	   os_dly_wait (1000);
	}
}
__task void phaseB (void)  
{
	int i = 0;
	for (;;) {
	    printf("task B %d\r\n",i++);
	   os_dly_wait (2000);
	}
}
__task void phaseC (void) 
{
	int i = 0;
	for (;;) {
	    printf("task C %d\r\n",i++);
	   os_dly_wait (3000);
	}
}
__task void phaseD (void)  
{
	int i = 0;
	fs_main();
	for (;;) {
	    printf("task D %d\r\n",i++);
	   os_dly_wait (500);
	}
}

__task void init (void)  
{

	t1 = os_tsk_create (phaseA, 1); /* start task phaseA     */
	os_dly_wait (50);
	t2 = os_tsk_create (phaseB, 1); /* start task phaseB      */
	os_dly_wait (50);
	t3 = os_tsk_create (phaseC, 1); /* start task phaseC       */
	os_dly_wait (50);
	t4 = os_tsk_create (phaseD, 0); /* start task phaseD       */
	os_tsk_delete_self ();
}

/*----------------------------------------------------------------------------
*        Main: Initialize and start RTX Kernel
*---------------------------------------------------------------------------*/

static void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;


	NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0);

	
	/* Configure one bit for preemption priority */
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);	
	
	// Enable the USART1 Interrupt
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

#if 0
	
	// Enable the USART3 Interrupt
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	

	// Enable the RTC Interrupt
	NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	

	// Enable the TIM2 Interrupt  //PWM play
	/*
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	*/
	
	// Enable the TIM4 Interrupt //AD record
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	/*//AD DMA
	NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel1_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	*/
	
	/*
	// Enable the USART2 Interrupt
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_Init(&NVIC_InitStructure);

	// Enable the WDG Interrupt
	NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	

	
	//EXTI_DeInit();
	
	
	NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 7;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQChannel;//TIM3_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	*/
	
	/*
	NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel7_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
	*/
	NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN_RX0_IRQChannel;//USB_LP_CAN1_RX0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN_TX_IRQChannel;//USB_HP_CAN1_TX_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
#endif	
}

char DirName[100];
char DirList[100*20];

int main(void)
{
	int i,j;// = 0;
	
	my_main();
///	SystemInit();
///	NVIC_Configuration();
///	GPIOX_Init();
///	Uart_Hint();
//	printf("OS_STKCHECK is %d\n",OS_STKCHECK);
	

///	fs_main();

	while(1)
	{
	}

	Rt_DirSet(NULL,DirName,100);
	i = Rt_DirGet(DirList,20,100,DirName);
	printf("find %d files\n",i);
	for(j = 0;j<i;j++)
	{
		printf("%s\n",&DirList[j*20]);
	}
	os_sys_init (init);
	
	#if 0
	//for(i = 0;i<1000;i++)
	while(1)
	{
		printf("hello %d\r\n",i++);
		sleep(10000);
	}
	//while(1);
	#endif
	
	return 0;
}



//end file

